package com.sqy.read;

import com.sqy.common.Num;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.*;

import java.io.*;
import java.util.List;

public class ReadPPT {
    public static void read(String path, String encoding) throws IOException {
        File file=new File(path);

        InputStream input = new FileInputStream(path);
        HSLFSlideShow hss = new HSLFSlideShow(input);
        //得到全部文本
        String pptText = readBySlideShowExtractor(hss);
        //System.out.println(pptText);
        //hss.getPictureData(); 返回此幻灯片的所有图片。
        List<HSLFPictureData> hslfPictureDataList = hss.getPictureData();
        //输出图片
         /*   for (HSLFPictureData hslfPictureData : hslfPictureDataList){
                byte[] data = hslfPictureData.getData();
                FileOutputStream out = new FileOutputStream("D:\\a大四上\\保密检查\\document\\文档\\Exam\\1\\" + UUID.randomUUID() + ".jpg");
                out.write(data);
                out.close();
            }*/

        //HSLFSlide 表示一张幻灯片对象
        //hss.getSlides(); 返回幻灯片中找到的所有普通幻灯片
        List<HSLFSlide> hslfSlideList = hss.getSlides();
        FileWriter writer;
        writer = new FileWriter(Num.PATH);

        for (HSLFSlide hslfSlide : hslfSlideList) {
            //hslfSlide.getHeadersFooters(); 此幻灯片的页眉/页脚
            HeadersFooters headersFooters = hslfSlide.getHeadersFooters();

            if (headersFooters.isDateTimeVisible() && headersFooters.isUserDateVisible()) {
                //System.out.println(headersFooters.getDateTimeText());
            }
            //headersFooters.getDateTimeFormat() 一个整数，指定用于设置日期时间样式的格式ID。
            //System.out.println(headersFooters.getDateTimeFormat());

            //HSLFShape表示工作表中包含的所有形状（幻灯片或注释）
            List<HSLFShape> hslfShapeList = hslfSlide.getShapes();

            for (HSLFShape hslfShape : hslfShapeList) {
                //System.out.println("形状类型："+hslfShape.getShapeType());
                if (hslfShape instanceof HSLFTextShape) {
                    //System.out.println("HSLFTextShape");
                    String text = ((HSLFTextShape) hslfShape).getText();
                    //  System.out.println(text);


                    String parentPath = file.getParent();
                    File parentDir = file.getParentFile();


                    writer.write(text);
                    writer.flush();


                }
                if (hslfShape instanceof HSLFTable) {
                    //       System.out.println("HSLFTable");
                    int rowSize = ((HSLFTable) hslfShape).getNumberOfRows();
                    int columnSize = ((HSLFTable) hslfShape).getNumberOfColumns();
                    for (int rowNum = 0; rowNum < rowSize; rowNum++) {
                        for (int columnNum = 0; columnNum < columnSize; columnNum++) {
                            HSLFTableCell cell = ((HSLFTable) hslfShape).getCell(rowNum, columnNum);
                            if (cell != null) {
                                String text = cell.getText();
                                //  System.out.println(text);


                                String parentPath = file.getParent();
                                File parentDir = file.getParentFile();

                                writer.write(text);
                                writer.flush();

                            }
                        }
                    }
                }
            }

        }
        ReadTxt.read(Num.PATH, encoding);
        writer.close();


    }



    /**
     * 得到文档的全部的文本,包括页脚，不包括批注
     *
     * @param slideShow ppt/pptx文档对象
     * @return 全部文本
     */
    public static String readBySlideShowExtractor(SlideShow slideShow) {
        SlideShowExtractor slideShowExtractor = new SlideShowExtractor(slideShow);
        return slideShowExtractor.getText();
    }

 


}
